<?php
namespace app\index\controller;

use service\DataService;
use think\Db;
use think\Controller;

class Index extends Controller
{
    public function search($keyword = ''){
        $goto_keyword = [
            '你们' => url('index/index/special'),
            '首页' => url('index/index/index'),
        ];

        if(isset($goto_keyword[$keyword])){
            header('Location: '.$goto_keyword[$keyword]);
        }

        $limit = 2;
        $articles = Db::table('article')
            ->where('name','like', '%'.$keyword.'%')
            ->whereOr('content','like', '%'.$keyword.'%')
            ->whereOr('keywords','like', '%'.$keyword.'%')
            ->order('id desc')
            ->paginate($limit,false,['path' => '/search-'.$keyword.'-[PAGE].html']);

        $this->assign('list', $articles);

        $categorys = Db::table('article_category')->where('parent_id','<>',0)->select();
        $this->assign('categorys', $categorys);

        //最新文章
        $new_limit = 3; //展示数量
        $new_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $this->assign('new_articles', $new_articles);

        //热门文章
        $new_limit = 3; //展示数量
        $hot_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $this->assign('hot_articles', $hot_articles);

        $limit = 99;
        $act = ['search_top','search_button','search_right_1','search_right_2','search_right_3'];
        $res = getAd($act,$limit);
        $ads = [];
        foreach ($res as $k => $v){
            $ads[$v['act']][] = $v;
        }
        $this->assign('ads', $ads);

        return $this->fetch('', ['keyword' => $keyword]);

    }

    public function index()
    {
        //是否手机端
        $is_mobile = isMobile();

        $limit = 99;
        $act = ['index_top','index_banner','index_mid','index_buttom_imgs','index_right','index_buttom_buttom'];
         $res = getAd($act,$limit);
         $ads = [];
         foreach ($res as $k => $v){
             $ads[$v['act']][] = $v;
         }

        $data['ads'] = $ads;

        $act = ['index_edit_recommend','index_new_message','index_top_mid','special_top_6','special_top_10','special_top_15','special_banner_6','special_banner_10','special_banner_15'];
        $limit = 99;
        $sort = 'a.id asc';
        $res = getArticleByTag($act, $limit, $sort);

        $article_top = [];
        foreach ($res as $k => $v){
            $article_top[$v['act']][] = $v;
        }

        $data['articles_top'] = $article_top;

        //楼层数据
        $ids = [6,15,10]; // 分类id
        $or_map['parent_id']=['in',$ids];

        $res = Db::table('article_category')
                ->whereIn('id', $ids)
                ->whereOr(function ($query) use ($or_map) {
                    $query->where($or_map);
                })
                ->where('status', 1)
                ->select();

        $res = formatData($res,'id', 'parent_id', 0, 'sub');
        foreach ($res as $k => &$v){
            if(count($v['sub'])>0){
                $category_ids = [];
                foreach ($v['sub'] as $kk => $vv){
                    $category_ids[] = $vv['id'];
                }
                //楼层第一列数据
                $articles = Db::table('article')->whereIn('category_id', $category_ids)->where('status', 1 )->order('id desc')->limit(6)->select();
                $v['articles'] = $articles;
                //楼层 热门文章  数据
                $articles = Db::table('article')->whereIn('category_id', $category_ids)->where('status', 1 )->order('look desc')->limit(10)->select();
                $v['hot_articles'] = $articles;

                $acts = ['special_top_'.$v['id'],'special_banner_'.$v['id']];
                $resss = getArticleByTag($acts, 99, 'a.id asc');

                $tmp = [];
                foreach ($resss as $kk => $vv){
                    $tmp[$vv['act']][] = $vv;
                }
                $v['mid_articles'] = $tmp;

            }
        }

        $data['articles_mid'] = $res;
        if($is_mobile){
            return $this->fetch('m_index',$data);
        }else{
            return $this->fetch('',$data);
        }
    }

    public function detail(){
        $id = request()->param('id/d',0);
        $article = Db::name('Article')->where('id',$id)->find();
        if(!$article){
            $this->error('文章不存在！');
        }
        $data['article'] = $article;
        $category_id = $article['category_id'];

        $prev = Db::name('Article')->where('category_id',$category_id)->where('id','<',$id)->order('id desc')->find();
        $last = Db::name('Article')->where('category_id',$category_id)->where('id','>',$id)->order('id asc')->find();
        $data['prev_article'] = $prev;
        $data['last_article'] = $last;

        $article_category = Db::table('article_category')->where('id',$category_id)->find();
        $parent_category = Db::table('article_category')->where('id',$article_category['parent_id'])->find();
        $categorys = Db::table('article_category')->where('parent_id',$article_category['parent_id'])->select();

        $data['category'] = $article_category;
        $data['parent_category'] = $parent_category;

        $category_ids = [];
        foreach ($categorys as $kk => $vv){
            $category_ids[] = $vv['id'];
        }
        $data['categorys'] = $categorys;

        //最新文章
        $new_limit = 3; //展示数量
        $new_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereIn('a.category_id', $category_ids)
            ->where('a.category_id','<>', $article_category['id'])
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $data['new_articles'] = $new_articles;

        //热门文章
        $new_limit = 3; //展示数量
        $hot_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereNotIn('a.category_id', $category_ids)
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $data['hot_articles'] = $hot_articles;

        //相关
        $new_limit = 3; //展示数量
        $xiangguan_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->where('a.category_id', $category_id)
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $data['xiangguan_articles'] = $xiangguan_articles;

        //相关推荐
        $new_limit = 3; //展示数量
        $xiangguan_re = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereIn('a.category_id', $category_ids)
            ->where('a.category_id','<>', $article_category['id'])
            ->where('a.status', 1 )
            ->limit($new_limit)
            ->select();
        $data['xiangguan_re'] = $xiangguan_re;

        $act = ['detail_top','detail_button','detail_right_1','detail_right_2','detail_right_3'];
        $res = getAd($act,99);
        $ads = [];
        foreach ($res as $k => $v){
            $ads[$v['act']][] = $v;
        }

        $data['ads'] = $ads;

        return $this->fetch('',$data);

    }

    public function special($id = 0)
    {
        $act = ['special_top','special_button','special_center','special_right_1','special_right_2','special_right_3'];
        $res = getAd($act,99);
        $ads = [];
        foreach ($res as $k => $v){
            $ads[$v['act']][] = $v;
        }

        $data['ads'] = $ads;

        $data['id']  =$id;
        $data['category_id'] = $id;
        $acts = ['special_top_'.$id,'special_banner_'.$id];
        $resss = getArticleByTag($acts, 99, 'a.id asc');

        $category = Db::table('article_category')->where('id', $id)->find();
        $this->assign('category',$category);
        $top = [];
        foreach ($resss as $kk => $vv){
            $top[$vv['act']][] = $vv;
        }
        $data['top'] = $top;


        //栏目分类
        $limit = 11; //展示分类的数据
        $article_limit = 4; //分类下文章的展示数量
        $category_ids = [];
        $categorys = Db::table('article_category')->where('parent_id', $data['category_id'])->where('status', 1)->limit($limit)->select();
        foreach ($categorys as $k => &$v){
            $articles = Db::table('article')->where('category_id', $v['id'])->where('status', 1 )->order('id desc')->limit($article_limit)->select();
            $v['articles'] = $articles;
            $category_ids[] = $v['id'];
        }
        $data['categorys'] = $categorys;

        //最新文章 【分类名称：category_name】
        $new_limit = 3; //展示数量
        $new_articles = Db::table('article')
                        ->alias('a')
                        ->field('a.*,ac.name as category_name,ac.url')
                        ->join('article_category ac', 'ac.id = a.category_id')
                        ->whereIn('a.category_id', $category_ids)
                        ->where('a.status', 1 )
                        ->order('a.id desc')
                        ->limit($new_limit)
                        ->select();
        $data['new_articles'] = $new_articles;

        //热门文章 【分类名称：category_name】
        $new_limit = 32; //展示数量
        $hot_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereNotIn('a.category_id', $category_ids)
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $data['hot_articles'] = $hot_articles;

        return $this->fetch('',$data);
    }

    public function articlelist($categoryid=0){

        $act = ['list_top','list_button','list_right_1','list_right_2'];
        $res = getAd($act,99);
        $ads = [];
        foreach ($res as $k => $v){
            $ads[$v['act']][] = $v;
        }

        $data['ads'] = $ads;
        $this->assign('ads', $ads);

        $limit = 3;
        $this->assign('id', $categoryid);

        $category = Db::table('article_category')->where('id', $categoryid)->where('status', 1)->find();
        $categorys = Db::table('article_category')->where('parent_id', $category['parent_id'])->where('status', 1)->select();

        $parent_category = Db::table('article_category')->where('id',$category['parent_id'])->find();
        $this->assign('parent_category', $parent_category);
        $this->assign('category', $category);

        $this->assign('categorys', $categorys);
        $category_ids = [];
        foreach ($categorys as  $k=>$v){
            if($v['id'] !=$categoryid){
                $category_ids[] = $v['id'];
            }
        }
        $articles = Db::table('article')
            ->where('category_id', $categoryid)
            ->where('status', 1)
            ->order('id desc')
            ->paginate($limit,false,['path' => '/ggg[PAGE].html']);

        $this->assign('list', $articles);

        //最新文章
        $new_limit = 3; //展示数量
        $new_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereIn('a.category_id', $category_ids)
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $this->assign('new_articles', $new_articles);

        //其他文章
        $category_ids[] = $categoryid;
        $new_limit = 31; //展示数量
        $other_articles = Db::table('article')
            ->alias('a')
            ->field('a.*,ac.name as category_name,ac.url')
            ->join('article_category ac', 'ac.id = a.category_id')
            ->whereNotIn('a.category_id', $category_ids)
            ->where('a.status', 1 )
            ->order('a.id desc')
            ->limit($new_limit)
            ->select();
        $this->assign('other_articles', $other_articles);
        $this->assign('categoryid', $categoryid);
        return $this->fetch();
    }


    public function company(){
        $limit = 1;
        $res = Db::table('company')
            ->where('status', 1)
            ->order('id desc')
            ->paginate($limit,false,['path' => '/company[PAGE].html']);

        $this->assign('list', $res);

        return $this->fetch();
    }

    public function companyDetail($id){

        $parent_id = -1;
        $res = getAreas($parent_id);
        print_r($res);

        dd();
        return $this->fetch();
    }


}
